package mtons.cast.modules.log;

import org.springframework.context.ApplicationContext;
import org.springframework.messaging.simp.SimpMessagingTemplate;

/**
 * Created by langhsu on 2017/10/13.
 */
public class Printer {
    public static ApplicationContext applicationContext;

    private static SimpMessagingTemplate simpMessagingTemplate;

    public static void info(String message) {
        System.out.println(message);

        sendToScoket(message);
    }

    public static void warn(String message) {
        System.out.println(message);

        sendToScoket(message);
    }


    private static void sendToScoket(String message) {
        if (simpMessagingTemplate == null && applicationContext != null) {
            SimpMessagingTemplate bean = applicationContext.getBean(SimpMessagingTemplate.class);
            if (bean != null) {
                simpMessagingTemplate = bean;
            }
        }

        if (simpMessagingTemplate != null) {
            simpMessagingTemplate.convertAndSend("/topic/notice", message);
        }
    }
}
